//获得装备时的震动效果
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.shakeInterval = 0
        this.shakeTime =0
        this.shakeTimeLimit = 0
        this.shakeFlag =false
        if(this.originPos){
            this.node.x = this.originPos.x
            this.node.y = this.originPos.y
        }
        //this.node.shake =this.shake
    },

    shake(secTime, fade,pos){
        this.shakeTime =0
        this.shakeInterval = 0 
        if(fade ===null){
            fade = true
        }
        this.fade = fade
        if(!secTime){
            secTime = 2
        }
        this.shakeTimeLimit =secTime
        this.shakeFlag = true
        if(!pos){
            pos = cc.v2(this.node.x,this.node.y)
        }
        this.pos =pos
        this.originPos =  cc.v2(this.node.x, this.node.y)

    },
    stop(){
        this.shakeFlag = false
    },
    update (dt) {
        this.shakeInterval +=dt
        if(this.shakeFlag && this.shakeTime<=this.shakeTimeLimit){
            this.shakeTime +=dt
        }else{
            this.start()

        }

        if(this.shakeInterval>0 && this.shakeFlag ){
            
            let mag = 3
            if(this.fade){
                mag = (1 - this.shakeTime/ this.shakeTimeLimit)*10
            }
            if(!mag){
                mag =0
            }
            let dirx = Math.random()<0.5?-1:1
            let diry = Math.random()<0.5?-1:1
            
            this.node.x = dirx * mag + this.pos.x
            this.node.y = diry * mag + this.pos.y
        }
    },
});
